package com.dxy.chatserver.db.entity;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.Setting;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "#{@indexName}")
@Setting(shards = 5)
public class EsOnlineMessageBuild{

    @Id
    private String id;

    @Field(name = "session_id",type=FieldType.Keyword)
    private String sessionId;

    @Field(name = "session_build",type=FieldType.Keyword)
    private String sessionBuild;

    @Field(name = "msg_id",type=FieldType.Keyword)
    private String msgId;

    @Field(name = "create_time",type=FieldType.Long)
    private Long createTime;

    @Field(name = "type",type= FieldType.Integer)
    private Integer type;

    @Field(name = "content",type= FieldType.Text,analyzer ="ik_max_word")
    private String content;

    @Field(name = "is_room_chat",type= FieldType.Integer)
    private Integer isRoomChat;

    @Field(name = "status",type= FieldType.Integer)
    private Integer status;

    @Field(name = "from_user",type= FieldType.Text,analyzer ="ik_max_word")
    private String fromUser;

    @Field(name = "to_user",type= FieldType.Text,analyzer ="ik_max_word")
    private String toUser;

    @Field(name = "bodies",type= FieldType.Text)
    private String bodies;

    public static EsOnlineMessageBuild Build(AcceptLineMsgResponse response,String collectionName){
        EsOnlineMessageBuild build = new EsOnlineMessageBuild();
        build.setId(collectionName +":"+response.getMsgId()+":"+response.getCreateTime());
        build.setSessionId(response.getSessionId());
        build.setSessionBuild(collectionName);
        build.setMsgId(response.getMsgId());
        build.setCreateTime(response.getCreateTime());
        build.setType(response.getType());
        build.setContent(response.getContent());
        build.setIsRoomChat(response.getIsRoomChat());
        build.setStatus(response.getStatus());
        build.setFromUser(JSONUtil.toJsonStr(response.getFromUser()));
        build.setToUser(JSONUtil.toJsonStr(response.getToUser()));
        build.setBodies(JSONUtil.toJsonStr(response.getBodies()));
        return build;
    }

    public static List<EsOnlineMessageBuild> Build(List<AcceptLineMsgResponse> response,String collectionName){
        List<EsOnlineMessageBuild> list = new ArrayList<>();
        response.forEach(o->list.add(Build(o,collectionName)));
        return list;
    }

    public static List<EsOnlineMessageBuild> Build(List<AcceptLineMsgResponse> response){
        List<EsOnlineMessageBuild> list = new ArrayList<>();
        response.forEach(o->list.add(Build(o,o.getSessionId())));
        return list;
    }
}
